package com.examination.system.instructor.service.impl;


import com.examination.framework.exception.ExceptionCast;
import com.examination.framework.model.response.CommonCode;
import com.examination.framework.model.response.QueryResponseResult;
import com.examination.framework.model.response.QueryResult;
import com.examination.system.administration.response.AddResult;
import com.examination.system.administration.response.DeleteResult;
import com.examination.system.administration.response.UpdateResult;
import com.examination.system.instructor.dao.BaseMapper;
import com.examination.system.instructor.response.CurrencyCode;
import com.examination.system.instructor.service.EntityRepositoryService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.lang.reflect.ParameterizedType;
import java.util.List;

/**
 * @author 寇淇芮
 * @Date 2019/10/15 19:42
 */
@Service
public class EntityRepositoryServiceImpl<M extends BaseMapper<T>,T>  implements EntityRepositoryService<T> {
    @Autowired
    protected M baseMapper;

    @Override
    public QueryResponseResult<T> findListPage(Integer page, Integer size,T entity) {

        final int minPage = 1;
        if (page<minPage){
            page = 1;
        }
        //设置页码和显示条数
        PageHelper.startPage(page,size);
        //查询数据
        Page<T> nationPage = baseMapper.selectAllPage(entity);

        //回写
        QueryResult queryResult = new QueryResult();
        //显示的记录
        queryResult.setList(nationPage.getResult());
        //一共的条数
        queryResult.setTotal(nationPage.getTotal());

        return new QueryResponseResult<T>(CommonCode.SUCCESS,queryResult);
    }

    @Override
    public T findById(String id) {
        this.judgeIdIsNull(id);

        return baseMapper.selectById(id);
    }

    @Override
    public UpdateResult<T> updateById(T entity, String id) {
        this.judgeData(entity,id);;
        return baseMapper.update(entity, id)>0 ? new UpdateResult<T>(CommonCode.SUCCESS,baseMapper.selectById(id)):new UpdateResult<T>(CommonCode.FAIL,null);
    }

    @Override
    public DeleteResult<T> deleteById( String id) {
        this.judgeIdIsNull(id);
        T t = baseMapper.selectById(id);
        return baseMapper.delectById(id)>0?new DeleteResult<T>(CommonCode.SUCCESS,t):new DeleteResult<T>(CommonCode.FAIL,null);
    }

    @Override
    public DeleteResult<T> deleteEentiy(T entity) {
        return baseMapper.delete(entity)>0?new DeleteResult<T>(CommonCode.SUCCESS,entity):new DeleteResult<T>(CommonCode.FAIL,null);
    }

    private void judgeData(T entity,String id){
        judgeObjectIsNull(entity);
        judgeIdIsNull(id);
    }


    @Override
    public AddResult<T> addEntity(T entity) {
        Integer saver = baseMapper.saver(entity);

        return new AddResult(CommonCode.SUCCESS,null);
    }


    private void  judgeObjectIsNull(T entity){
        if(entity==null){
            ExceptionCast.cast(CurrencyCode.DataCannotBeEmpty);
        }
    }
    /**
     * 判断Id是否为空
     * @param id
     */
    private  void judgeIdIsNull (String id){
        if(StringUtils.isEmpty(id)){
            ExceptionCast.cast(CurrencyCode.DataCannotBeEmpty);
        }
    }
}
